/*
 * @Author: hongbin
 * @Date: 2023-08-09 16:46:03
 * @LastEditors: hongbin
 * @LastEditTime: 2023-08-11 17:33:57
 * @Description:环形圆点
 */

import { DotMaterial } from "@/shader/DotMaterial";
import * as THREE from "three";

export class RingPoint {
    math: THREE.Points<
        THREE.BufferGeometry<THREE.NormalBufferAttributes>,
        DotMaterial
    >;

    constructor(
        radius = 11,
        count = 90,
        private color = new THREE.Color("#A69D98")
    ) {
        const circleGeometry = new THREE.CircleGeometry(radius, count);

        const { array } = (
            circleGeometry.getAttribute("position") as THREE.BufferAttribute
        ).toJSON();

        array.shift();
        array.shift();
        array.shift();

        const geometry = new THREE.BufferGeometry();

        geometry.setAttribute(
            "position",
            new THREE.Float32BufferAttribute(array, 3)
        );

        this.setSizeAttr(geometry);

        this.math = new THREE.Points(
            geometry,
            // new THREE.PointsMaterial({ size: 0.2 })
            new DotMaterial(
                {
                    near: 30,
                    far: -5,
                    maxOpacity: 0.6,
                    fadeDistance: 5,
                },
                {
                    USE_ACOLOR: true,
                    depthTest: false,
                }
            )
        );
    }

    setSizeAttr(geometry: THREE.BufferGeometry) {
        const pointCount = geometry.getAttribute("position").count;

        const size = new Float32Array(pointCount);
        const aColor = new Float32Array(pointCount * 3);

        for (let i = 0; i < pointCount; i++) {
            size[i] = (Math.random() * 0.5 + 0.5) * 1.5;
            const i3 = i * 3;
            aColor[i3] = this.color.r;
            aColor[i3 + 1] = this.color.g;
            aColor[i3 + 2] = this.color.b;
        }

        const bufferAttr = new THREE.BufferAttribute(size, 1);
        geometry.setAttribute("size", bufferAttr);

        geometry.setAttribute("aColor", new THREE.BufferAttribute(aColor, 3));
    }
}
